package tq.com.tqandroidim.inner;

import org.json.JSONException;
import org.json.JSONObject;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.charset.Charset;
import java.util.Date;

import tq.com.tqandroidim.TQIMMsg;

import static tq.com.tqandroidim.inner.TQIMMsgType.IM_TYPE_RECV;
import static tq.com.tqandroidim.inner.TQIMMsgType.IM_TYPE_SEND;

public class TQImPakage {
    private int pkgId;
    private int bodyLen;

    private int commandType;
    private JSONObject mJSONObject;

    static public int PAKGE_HEADER_LEN = 14;

    static public TQImPakage decodeHeader(ByteBuffer cache){
        cache.order(ByteOrder.BIG_ENDIAN);
        byte[] ff = new byte[2];
        cache.get(ff);

        String flag = new String(ff, Charset.forName("utf-8"));
        if(flag.compareToIgnoreCase("ff") != 0){
            return null;
        }
        int len = cache.getInt();
        int type = cache.getInt();
        int id = cache.getInt();

        TQImPakage pkg = new TQImPakage();
        pkg.bodyLen = len;
        pkg.commandType = type;
        pkg.pkgId = id;

        return pkg;
    }
    static void encodeHeader(ByteBuffer cahce, int type, int len, int id){
        cahce.order(ByteOrder.BIG_ENDIAN);
        byte lB = 'f';
        cahce.put(lB);
        cahce.put(lB);

        cahce.putInt(len);
        cahce.putInt(type);
        cahce.putInt(id);
    }
    public TQImPakage(){}
    public TQImPakage(TQIMMsg pMsg){
        try {
            JSONObject lObject = new JSONObject();

            lObject.put("ruid", pMsg.getTo());
            lObject.put("rgid", pMsg.getToGroup());
            lObject.put("suid", pMsg.getFrom());

            lObject.put("ctype", pMsg.getContentType());
            lObject.put("c", pMsg.getContent());

            mJSONObject = lObject;

            commandType = IM_TYPE_SEND;
            pkgId = 0;//(int)newPkgId();

        }catch (JSONException pE){
            pE.printStackTrace();
        }


    }
    public ByteBuffer encode(){
        String strBody = null;
        if(mJSONObject != null){
            strBody = mJSONObject.toString();
        }
        byte[] body = null;
        if(strBody != null){
            Charset lCharset = Charset.forName("utf-8");
            body = strBody.getBytes(lCharset);
            bodyLen = body.length;
        }

        ByteBuffer cache = ByteBuffer.allocate(PAKGE_HEADER_LEN + bodyLen);
        encodeHeader(cache, commandType, bodyLen, pkgId);

        cache.put(body);
        return cache;
    }

    public void setBody(JSONObject pBody){
        mJSONObject = pBody;
    }

    public JSONObject getBody(){
        return mJSONObject;
    }

    public void decodeBody(ByteBuffer body){
        if(body == null) return;

        String strBody = new String(body.array(), Charset.forName("utf-8"));
        if(strBody == null || strBody.length() == 0) return;

        try {
            mJSONObject = new JSONObject(strBody);
        }catch (JSONException pE){
            pE.printStackTrace();
        }
    }

    public int getPkgId() {
        return pkgId;
    }

    public void setPkgId(int pPkgId) {
        pkgId = pPkgId;
    }

    public int getBodyLen() {
        return bodyLen;
    }

    public void setBodyLen(int pBodyLen) {
        bodyLen = pBodyLen;
    }

    public int getCommandType() {
        return commandType;
    }

    public void setCommandType(int pCommandType) {
        commandType = pCommandType;
    }

    static long newPkgId(){
        Date lDate = new Date();
        return lDate.getTime();
    }
    static public class TQImSlite{
        public ByteBuffer cache;
        public int limitRead;
    }
}
